﻿#region Using
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion Using

public enum ScreenFlashType
{
    ZoomAndFade,
    ZoomWithTrailAndFade,
    ZoomAndExit,
    PowerUpCounter
}

namespace LloydGame
{
    public class ScreenFlash
    {
        #region Fields
        protected Game m_game;
        protected ScreenFlashType m_animation;
        protected float m_animation_length;
        protected float m_animation_time;
        protected bool m_animation_over;
        protected float m_scale;
        protected Texture2D m_sprite0;
        protected String m_soundcue;
        public int m_type;
        #endregion Fields

        #region Properties
        public bool Done { get { return m_animation_over; } }
        public ScreenFlashType animation { get { return m_animation; } }
        #endregion

        #region Constructor
        public ScreenFlash(Game p_game, Texture2D p_sprite0, String p_soundcue, ScreenFlashType p_animation, float p_animation_length, float p_scale)
        {
            m_game = p_game;
            m_sprite0 = p_sprite0;
            m_soundcue = p_soundcue;
            m_animation = p_animation;
            m_animation_length = p_animation_length;
            m_animation_time = 0.0f;
            m_animation_over = false;
            m_scale = p_scale;

            if (m_soundcue != null)
            {
                ((Game1)p_game).soundBank.PlayCue(m_soundcue);
            }
        }

        public ScreenFlash(Game p_game, Texture2D p_sprite0, float p_animation_length, int p_type)
        {
            m_game = p_game;
            m_sprite0 = p_sprite0;
            m_soundcue = null;
            m_animation = ScreenFlashType.PowerUpCounter;
            m_animation_length = p_animation_length;
            m_animation_time = 0.0f;
            m_animation_over = false;
            m_scale = 1.0f;
            m_type = p_type;
        }
        #endregion

        #region Update
        public virtual void Update(GameTime p_gametime)
        {
            m_animation_time += (float)p_gametime.ElapsedGameTime.Milliseconds / 1000.0f;

            // Self removeal if animation is over
            if (m_animation_time >= m_animation_length)
            {
                m_animation_over = true;
            }
        }
        #endregion

        #region Draw
        public virtual void Draw(SpriteBatch p_spritebatch)
        {
            if (m_animation == ScreenFlashType.ZoomAndFade)
            {
                // Zoom the sprite and fade it out
                float t_interp = m_animation_time / m_animation_length;
                Vector2 t_pos = new Vector2(m_game.GraphicsDevice.Viewport.Width / 2.0f - (m_sprite0.Width * t_interp * m_scale) / 2.0f,
                    m_game.GraphicsDevice.Viewport.Height / 2.0f - (m_sprite0.Height * t_interp * m_scale) / 2.0f);
                p_spritebatch.Draw(m_sprite0, t_pos, null, new Color(Color.White, 1.0f - t_interp), 0.0f, Vector2.Zero, t_interp * m_scale, SpriteEffects.None, 0.0f);
            }
            else if (m_animation == ScreenFlashType.ZoomWithTrailAndFade)
            {
                // Zoom the sprite with a trail, and fade it out
                float t_interp_org = m_animation_time / m_animation_length;
                float t_interp = t_interp_org;
                Vector2 t_pos;
                for (int i = 3; i >= 0; --i)
                {
                    t_pos = new Vector2(m_game.GraphicsDevice.Viewport.Width / 2.0f - (m_sprite0.Width * t_interp * m_scale) / 2.0f,
                    m_game.GraphicsDevice.Viewport.Height / 2.0f - (m_sprite0.Height * t_interp * m_scale) / 2.0f);
                    p_spritebatch.Draw(m_sprite0, t_pos, null, new Color(Color.White, 1.0f - t_interp), 0.0f, Vector2.Zero, t_interp * m_scale, SpriteEffects.None, 0.0f);
                    t_interp = t_interp_org + 0.2f * (float)i;
                }

            }
            
        }

        // Special case for power-up counters
        public virtual void Draw(SpriteBatch p_spritebatch, int p_x)
        {
            // Power-up count down "clock"
            int t_draw_x = m_game.GraphicsDevice.Viewport.Width - 70;
            int t_draw_y = m_game.GraphicsDevice.Viewport.Height - p_x * 70 - 70;
            Vector2 t_pos = new Vector2(t_draw_x, t_draw_y);

            // Icon
            p_spritebatch.Draw(m_sprite0, t_pos, new Rectangle(64 * m_type, 320, 64, 64), Color.White, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0);

            // Overlay (32 frames of overlay "animation")
            float t_interp = m_animation_time / m_animation_length;
            float t_alpha = (t_interp * 32.0f) - (float)Math.Floor((float)(t_interp * 32.0f));
            int t_frame_under = (int)(t_interp * 32.0f);
            int t_frame_over = t_frame_under + 1;

            // First
            int t_sprite_y = (int)Math.Floor((float)(t_frame_under / 8));
            int t_sprite_x = t_frame_under % 8;
            p_spritebatch.Draw(m_sprite0, t_pos, new Rectangle(t_sprite_x * 64, t_sprite_y * 64, 64, 64), Color.White, 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0);

            // Second
            if (t_frame_over < 33)
            {
                t_sprite_y = (int)Math.Floor((float)(t_frame_over / 8));
                t_sprite_x = t_frame_over % 8;
                p_spritebatch.Draw(m_sprite0, t_pos, new Rectangle(t_sprite_x * 64, t_sprite_y * 64, 64, 64), new Color(Color.White, t_alpha), 0.0f, Vector2.Zero, 0.8f, SpriteEffects.None, 0);
            }
        }

        #endregion Draw

    }
}
